package main

import (
	"fmt"
	"time"
)

// 寻找字符串中，最长且不重复的字符串长度
func lengthNoRepeatStringInternation(str string) int {
	lastOccurred := make(map[rune]int)
	maxLength := 0
	start := 0
	t1 := time.Now()
	for i, ch := range []rune(str) {
		if lastI, ok := lastOccurred[ch]; ok && lastI >= start {
			start = lastOccurred[ch] + 1
		}
		if i-start+1 > maxLength {
			maxLength = i - start + 1
		}
		lastOccurred[ch] = i
	}
	t2 := time.Now()
	fmt.Println(t2.Sub(t1))
	return maxLength
}

// 寻找字符串中，最长且不重复的字符串长度
func lengthNoRepeatString(str string) int {
	lastOccurred := make(map[byte]int)
	maxLength := 0
	start := 0
	t1 := time.Now()
	for i, ch := range []byte(str) {
		if lastI, ok := lastOccurred[ch]; ok && lastI >= start {
			start = lastOccurred[ch] + 1
		}
		if i-start+1 > maxLength {
			maxLength = i - start + 1
		}
		lastOccurred[ch] = i
	}
	t2 := time.Now()
	fmt.Println(t2.Sub(t1))
	return maxLength
}

func main() {
	fmt.Println(lengthNoRepeatString("6655+++555655++++++**/-//**//"))
	fmt.Println(lengthNoRepeatStringInternation("我爱中国，因为这里是我的家乡"))
	fmt.Println(lengthNoRepeatStringInternation("贵州多彩宝互联网服务有限公司"))
}
